<extend name="Public/base"/>

<block name="body">
  <!-- 标题栏 -->
  <div class="main-title">
    <h2>购票列表</h2>
  </div>

  <!-- 数据列表 -->
  <div class="data-table buytickets-list">
    <div class="data-table table-striped">
      <table>
        <!-- 表头 -->
        <thead>
          <tr>
            <th>展览</th>
            <th>票务名称</th>
            <th>单价/元</th> 
            <th>开始日期</th>
            <th>结束日期</th>
            <th>库存/张</th>
            <th>购票</th>
          </tr>
        </thead>

        <!-- 列表 -->
        <tbody>
          <volist name="list_data" id="data">
            <tr>
              <td>{$data.ex_name}</td>
              <td>{$data.name}</td>
              <td>{$data.price}</td>
              <td>{$data.begin_date}</td>
              <td>{$data.end_date}</td>
              <td>{$data.stock}</td>
              <td><button class="btn btn-primary" onclick="scanOpen({$data.id})">购买</button></td> 
            </tr>
          </volist>
        </tbody>
      </table>
    </div>
  </div>
  <div class="scancover buyticket">
    <div class="box">
      <i class="fa fa-close" onclick="scanClose()"></i>
      <!-- <div class="info">
        <span><b>展览：</b>ddd</span>
        <span><b>票务名称：</b>sss</span>
        <span><b>单价：</b></span>
      </div> -->
      <p style="padding:20px 0 10px;font-size: 14px;">输入购买张数：</p>
      <div class="putin">
        <input type="number" place="购买张数" id="scaninput">
      </div>
      <div class="payment">
        <p>支付方式：</p>
        <label for="wechat"><input type="radio" name="payment" value="1" id="wechat">微信支付</label>
        <label for="alipay"><input type="radio" name="payment" value ="2" id="alipay">支付宝支付</label>
        <label for="cash"><input type="radio" name="payment" value ="3" id="cash">现金支付</label>
      </div>
      <p style="padding-top: 10px;font-size: 16px;">总价：<span class="total">0</span> 元</p>
      <button class="btn btn-primary" onclick="buy()">购买</button>
    </div>
  </div>
</block>

<block name="script">
<script type="text/javascript">
var ticket = null;
function exchangeRedeemcode(redeem_id){
  $.ajax({
    type : 'post',
    url : 'index.php?s=Admin/Redeemcode/exchangeRedeemcode',
    data : { id : redeem_id},
    success: function(data){
      console.log(data);
      window.location.reload();
    }
  })
}

function scanOpen(ticket_id){
  $.ajax({
    type : 'get',
    url : 'index.php?s=Admin/Buytickets/ticketinfo&ticketId='+ticket_id,
    success: function(data){
      ticket = data.info;
      $('.scancover .box').prepend("<div class='info'><span><b>展览：</b>"+ticket.ex_name+"</span><span><b>票务名称：</b>"+ticket.name+"</span><span><b>单价：</b>"+ticket.price+"/元</span></div>");
      $('.scancover').show();
      $('#scaninput').focus();    
    }
  })
  
}

function scanClose(){
  $('#scaninput').val('');
  $("input[type='radio']").removeAttr("checked");
  $('.info').remove();
  $(".total").replaceWith("<span class='total'>0</span>");
  $('.scancover').hide();
  ticket = null;
}

$('#scaninput').bind('input propertychange', function() {
  var ticketNum = $('#scaninput').val();
  if(ticketNum<0){
    $('#scaninput').val('');
  }else{
    $(".total").replaceWith("<span class='total'>"+ticket.price*ticketNum+"</span>");
  }
});

function buy(){
  var payment = $("input[type='radio']:checked").val();
  var ticketNum = $('#scaninput').val();
  if(!payment){
    alert('请选择支付方式！');
  }else if(!ticketNum){
    alert('请输入购买张数！');
  }
  else{
    if(confirm('是否确认购买？')){
      $.ajax({
        type : 'post',
        url : 'index.php?s=Admin/Buytickets/buy',
        data: {
          ticket_id: ticket.id,
          amount: ticketNum,
          payment: payment
        },
        success: function(data){
          if(data.status==0){
            alert(data.info);
          }else{
            alert('购买成功！');
            history.go(0);
          }
        }
      });
    } 
  }
}
    
  // });
</script>
</block>
